package iamjason.display 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.display.DisplayObjectContainer;
	/**
	 * ...
	 * @author Jason Tseng
	 */
	public class BaseMovieClip extends MovieClip {
		private var _destroyed:Boolean = false;
		public function BaseMovieClip() {
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		private function addedToStage(e:Event):void {
			this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
			init();
		}
		protected function init():void {
		}
		private function removedFromStage(e:Event):void {
			destroy();
		}
		public function destroy():void {
			if (_destroyed)
				return;
			this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
			try {
				DisplayObjectContainer(parent).removeChild(this);
			} catch(e:Error) {}
		}
	}

}